from rest_framework import serializers

from areas.models import Area
from goods.models import GoodsAttribute, Goods
from orders.models import OrderInfo, DiscountCoupon, Courier_Company, OrderGoods, ReturnDetail, Reason, \
    Business_Address, OrderInfoUpdate, GoodsList
from orders.serializers import Address_serializer
from users.models import User, Feedback


def time_format():
    """时间格式化"""
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    update_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    return create_time, update_time


class GoodsSerialier(serializers.ModelSerializer):
    """商品属性序列化器"""

    class Meta:
        model = Goods
        fields = ("id", 'name',)


class GoodsAttribute_Serialier(serializers.ModelSerializer):
    """订单商品属性序列化器"""
    goods = GoodsSerialier()

    class Meta:
        model = GoodsAttribute
        fields = ('goods', "weight", 'color', 'goods_code', 'size', 'image',)
        read_only_fields = ('goods',)


class Reason_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Reason
        fields = ('explain',)


class ReturnDetail_Serialier(serializers.ModelSerializer):
    """退换货细节"""
    apply_time, check_time = time_format()
    reason = Reason_Serializer()

    class Meta:
        model = ReturnDetail
        fields = (
            "reason", "introduce", "return_numbers", "numbers", "company", "apply_time", "check_time", "check_user",
            "check_results", "check_reason", "return_id", "return_money", "return_address_id")


class OrderGoodssSerialier(serializers.ModelSerializer):
    """查询订单包含商品序列化器"""
    goods = GoodsAttribute_Serialier()
    return_reason = ReturnDetail_Serialier(many=True, read_only=True)

    class Meta:
        model = OrderGoods
        fields = ("id", "goods", "price", "count", "is_return", "return_reason",)


class Area_Serializer(serializers.ModelSerializer):
    """省市区划分"""

    class Meta:
        model = Area
        fields = ("name",)


class User_serializer(serializers.ModelSerializer):
    """订单收货地址序列化器"""

    class Meta:
        model = User
        fields = ("id", "nickname",)


class Business_AddressSerialier(serializers.ModelSerializer):
    province = Area_Serializer()
    city = Area_Serializer()
    district = Area_Serializer()

    class Meta:
        model = Business_Address
        fields = ("name", "person", "phone_number", "province", "city", "district", "place")


class OrderInfoUpdate_serializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    check_user = User_serializer()

    class Meta:
        model = OrderInfoUpdate
        fields = (
            "create_time", "check_user", "new", "old_receiver", "update_receiver", "old_address", "update_address",
            "old_mobile", "update_mobile", "old_total_amount", "update_total_amount", "old_freight", "update_freight")


class DiscountCouponSerialier(serializers.ModelSerializer):
    class Meta:
        model = DiscountCoupon
        fields = ('name', "value", 'full',)


class Courier_Company_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Courier_Company
        fields = ('name',)


class OrderInfoSerializer(serializers.ModelSerializer):
    "订单列表查询序列化器business_address"
    create_time, update_time = time_format()
    pay_time, send_time = time_format()
    obtain_time, success_time = time_format()
    goods = OrderGoodssSerialier(many=True, required=False)
    business_address = Business_AddressSerialier(required=False, )
    user = User_serializer()
    address = Address_serializer()
    orderinfoupdate = OrderInfoUpdate_serializer(many=True, required=False)
    discountcoupon = DiscountCouponSerialier(read_only=True, )
    courier_company = Courier_Company_Serializer()

    class Meta:
        model = OrderInfo
        fields = (
            "order_id", "user", "create_time", "pay_time", "send_time", "obtain_time", "success_time", "update_time",
            "over_time", "total_count", "total_amount", "freight", "pay_method", "status", "post_script", "numbers",
            "is_show", "check_script", "discountcoupon", "goods", "address", "business_address", "courier_company",
            "orderinfoupdate")


class OrderInfoListSerializer(serializers.ModelSerializer):
    create_time, update_time = time_format()
    user = User_serializer()
    goods = OrderGoodssSerialier(many=True, required=False)
    address = Address_serializer()
    orderinfoupdate = OrderInfoUpdate_serializer(many=True, required=False)

    class Meta:
        model = OrderInfo
        fields = ("order_id", "freight", "user", "create_time", "update_time", "total_count", "total_amount", "status",
                  "numbers", "check_script", "goods", "address", "orderinfoupdate",)


class OrderSerialier(serializers.ModelSerializer):
    create_time, update_time = time_format()
    pay_time, send_time = time_format()
    obtain_time, success_time = time_format()
    user = User_serializer()
    business_address = Business_AddressSerialier(required=False, )
    orderinfoupdate = OrderInfoUpdate_serializer(many=True, required=False)
    address = Address_serializer()
    courier_company = Courier_Company_Serializer()

    class Meta:
        model = OrderInfo
        fields = (
            "order_id", "over_time", "discountcoupon", "create_time", "send_time", "pay_time", "obtain_time",
            "success_time", "update_time", "over_time", "total_amount", "freight", "status", "numbers", "user",
            "business_address", "orderinfoupdate", "address", "courier_company", "check_script")


class OrderGoodsIsReturnSerializer(serializers.ModelSerializer):
    goods = GoodsAttribute_Serialier()
    order = OrderSerialier()
    return_reason = ReturnDetail_Serialier(many=True, read_only=True)

    class Meta:
        model = OrderGoods
        fields = ("id","price", "count", "is_return", "order", "goods", "return_reason",)


class OrderInfoPutSerializer(serializers.ModelSerializer):
    """修改订单序列化器"""
    update_address = serializers.CharField(write_only=True, required=False, )
    update_receiver = serializers.CharField(write_only=True, required=False, )
    update_mobile = serializers.CharField(write_only=True, required=False, )
    update_total_amount = serializers.FloatField(write_only=True, required=False, )

    class Meta:
        model = OrderInfo
        fields = (
            "update_total_amount", "business_address", "check_script", "update_receiver", "update_address",
            "update_mobile", "update_total_amount", "status")


class ReturnDetailSerializer(serializers.ModelSerializer):
    class Meta:
        model = ReturnDetail
        fields = ("id", "check_results", "check_reason")


class Order_GoodsSerializer(serializers.ModelSerializer):
    check = serializers.BooleanField( write_only=True)
    return_reason = ReturnDetailSerializer(many=True, read_only=True)
    check_reason = serializers.CharField(write_only=True, default=None)

    class Meta:
        model = OrderGoods
        fields = ("id",  "is_return", "check", "return_reason", "check_reason")


class GoodsListSerialier(serializers.ModelSerializer):
    cate_goods = GoodsSerialier()

    class Meta:
        model = GoodsList
        fields = ("id", "cate_goods",)


class DiscountCouponSerializer(serializers.ModelSerializer):
    "查询优惠券序列化器"
    detail_name = serializers.CharField(required=False, write_only=True)
    get_url = serializers.URLField(read_only=True)
    is_show = serializers.BooleanField(default=False, )
    goods_list = GoodsListSerialier(many=True, read_only=True)
    goodslist = serializers.ListField(write_only=True, required=False)
    user = User_serializer(read_only=True)

    class Meta:
        model = DiscountCoupon
        fields = (
            "id", "detail_name", "name", "status", "start_time", "create_time", "used_time", "end_time", "value",
            'category', "user", "status", "get_url", "is_show", "is_delete",
            "full", "category", "launch_counts", "issuance_counts", "used_counts", "goods_list", "goodslist")


class DiscountCouponPostSerializer(serializers.ModelSerializer):
    "增加优惠券模板序列化器"
    create_time, update_time = time_format()
    start_time = serializers.DateField(required=True, )
    end_time = serializers.DateField(required=True, )
    name = serializers.CharField(required=True, )
    category = serializers.IntegerField(required=True, max_value=3, min_value=1)
    value = serializers.FloatField(required=True, )
    launch_counts = serializers.IntegerField(required=True, )
    full = serializers.IntegerField(required=True, )
    goods_list = serializers.ListField(child=serializers.IntegerField(), required=False, write_only=True, default=None)
    is_show = serializers.BooleanField(write_only=True)

    class Meta:
        model = DiscountCoupon
        fields = (
            "id", "name", "value", "full", "launch_counts", "create_time", "update_time", "start_time", "end_time",
            "is_show", "category", "goods_list",)


class DiscountCouponPutSerializer(serializers.ModelSerializer):
    id = serializers.IntegerField(min_value=1, required=False)
    start_time = serializers.DateField(required=False, )
    end_time = serializers.DateField(required=False, )
    name = serializers.CharField(required=False, )
    goods_list = GoodsListSerialier(many=True, read_only=True)

    class Meta:
        model = DiscountCoupon
        fields = (
            "id", "name", "category", "is_show", "value", "full", "launch_counts", "start_time", "end_time",
            "goods_list")


class Courier_CompanySerializer(serializers.ModelSerializer):
    "快递公司增删改查序列化器"

    class Meta:
        model = Courier_Company
        fields = ("id", "price", "name",)


class Courier_CompanyPutSerializer(serializers.ModelSerializer):
    "修改快递公司序列化器"
    name = serializers.CharField(required=False, )
    price = serializers.FloatField(required=False, )

    class Meta:
        model = Courier_Company
        fields = ("id", "price", "name",)


class ReasonSerializer(serializers.ModelSerializer):
    "退换货原因"

    class Meta:
        model = Reason
        fields = ("id", "explain", "is_enabled", "return_type",)
        read_only_field = ("id",)


class ReasonUpdateSerializer(serializers.ModelSerializer):
    "修改退换货原因"
    return_type = serializers.IntegerField(required=False, )
    explain = serializers.CharField(required=False, )

    class Meta:
        model = Reason
        fields = ("id", "explain", "is_enabled", "return_type",)
        read_only_field = ("id",)


class Business_AddressUpdateSerializer(serializers.ModelSerializer):
    place = serializers.CharField(required=False)
    phone_number = serializers.CharField(required=False)
    person = serializers.CharField(required=False)
    name = serializers.CharField(required=False)

    class Meta:
        model = Business_Address
        fields = ("id", "name", "province", "city", "district", "place", "phone_number", "person",)
        read_only_field = ("id",)


class Business_AddressSerializer(serializers.ModelSerializer):
    province = Area_Serializer(read_only=True)
    city = Area_Serializer(read_only=True)
    district = Area_Serializer(read_only=True)
    province_id = serializers.IntegerField(write_only=True)
    city_id = serializers.IntegerField(write_only=True)
    district_id = serializers.IntegerField(write_only=True)

    class Meta:
        model = Business_Address
        fields = (
            "id", "name", "province", "province_id", "city", "district", "district_id", "city_id", "place",
            "phone_number", "person",)


class OrderSendOutSerializer(serializers.ModelSerializer):
    goods = OrderGoodssSerialier(many=True, required=False)
    address = Address_serializer()
    orderinfoupdate = OrderInfoUpdate_serializer(many=True, required=False)

    class Meta:
        model = OrderInfo
        fields = (
        "order_id", "total_count", "post_script", "numbers", "check_script", "goods", "address", "orderinfoupdate")


class OrderSendOut_PutSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderInfo
        fields = ("numbers",)


class FeedbackSerializer(serializers.ModelSerializer):
    create_time = serializers.DateTimeField(format="%Y-%m-%d %H:%M:%S", required=False, read_only=True)
    user = User_serializer()

    class Meta:
        model = Feedback
        fields = ("create_time", "user", "suggestion_id", "suggestion_text")


class GoodsAttribute_ChangeSerializer(serializers.ModelSerializer):
    goods = GoodsSerialier()
    class Meta:
        model = GoodsAttribute
        fields = ("id","goods_code", "color", "image", "size","weight","price","goods",)


class Get_Business_AddressSerializer(serializers.ModelSerializer):
    class Meta:
        model = Business_Address
        fields = ("id","name", )

